package com.example.smartall.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.example.smartall.R;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.entity.ServiceBean;
import com.example.smartall.ui.HomeActivity;
import com.example.smartall.ui.SubwayActivity;
import com.example.smartall.utils.TextUtils;

import java.util.List;
import java.util.Map;

public class ServiceRecycleAdapter extends RecyclerView.Adapter<ServiceRecycleAdapter.ViewHolder> {

    private Context context;
    private List<ServiceBean.RowsBean> beans;
    private OnItemClickedListener listener;
    //又来玩了
    private Map<String,Class> params;

    public ServiceRecycleAdapter(Context context, List<ServiceBean.RowsBean> beans) {
        this.context = context;
        this.beans = beans;
    }

    public ServiceRecycleAdapter(Context context, List<ServiceBean.RowsBean> beans, OnItemClickedListener listener) {
        this.context = context;
        this.beans = beans;
        this.listener = listener;
    }

    public void setListener(OnItemClickedListener listener) {
        this.listener = listener;
    }

    public void setParams(Map<String, Class> params) {
        this.params = params;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.service_item,null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (!TextUtils.isEmpty(beans.get(position).getImgUrl())){
            Glide.with(context)
                    .load(ApiConfig.BASE_URL + beans.get(position).getImgUrl())
                    .into(holder.serviceIcon);
        }else {
            Glide.with(context)
                    .load(context.getDrawable(R.mipmap.ic_launcher))
                    .transform(new CircleCrop())
                    .into(holder.serviceIcon);
        }
        holder.serviceName.setText(beans.get(position).getServiceName());
    }

    @Override
    public int getItemCount(){
        return beans.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView serviceIcon;
        private TextView serviceName;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            serviceIcon = (ImageView) itemView.findViewById(R.id.service_icon);
            serviceName = (TextView) itemView.findViewById(R.id.service_name);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null){
                        listener.onClick(getAdapterPosition(),beans.get(getAdapterPosition()).getServiceName());
                    }
                    if (params != null){
                        if (params.containsKey(beans.get(getAdapterPosition()).getServiceName())){
                            context.startActivity(new Intent(context,params.get(beans.get(getAdapterPosition()).getServiceName())));
                        }
                    }
                }
            });
        }
    }
    //回调
    public interface OnItemClickedListener{
        //根据位置或者服务名称
        void onClick(int position,String serviceName);
    }
}
